package com.example.eureka_client.service.impl;

import com.example.eureka_client.entity.UacUser;
import com.example.eureka_client.mapper.UacUserMapper;
import com.example.eureka_client.service.IUacUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.eureka_client.utils.PageRequest;
import com.example.eureka_client.utils.PageResult;
import com.example.eureka_client.utils.PageUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author plj
 * @since 2022-03-24
 */
@Service
public class UacUserServiceImpl extends ServiceImpl<UacUserMapper, UacUser> implements IUacUserService {
    @Override
    public UacUser getByLoginName(String loginName){
        return baseMapper.getByLoginName(loginName);
    }

    @Override
    public PageResult getUserList(List<String> userIds, PageRequest pageRequest){
        List<UacUser> uacUsers = baseMapper.getAttentionUserList(userIds);

        int pageNum = pageRequest.getPageNum(), pageSize = pageRequest.getPageSize();
        Page page = new Page(pageNum, pageSize);

        int total = uacUsers.size();
        page.setTotal(total);
        int startIndex = (pageNum - 1) * pageSize;
        int endIndex = Math.min(startIndex + pageSize, total);

        if (startIndex > endIndex) {
            page.addAll(new ArrayList());
            PageInfo pageInfo = new PageInfo<>(page);
            return PageUtils.getPageResult(pageRequest,pageInfo);
        } else {
            page.addAll(uacUsers.subList(startIndex, endIndex));
            PageInfo pageInfo = new PageInfo<>(page);
            return PageUtils.getPageResult(pageRequest,pageInfo);
        }
    }
}
